const app = getApp()
const $api = require('../../utils/api.js').API;

Page({
    data: {
        autoplay: !0,
        interval: 3e3,
        duration: 1e3,
        title: "",
        banner:'',
        imgUrl:app.globalData.imgUrl,
        list:{},
        page:1,
        pageShow:true
    },
    onLoad: function() {
        wx.showLoading()
        this.getBanner();
        this.getNes();
    },
    getBanner:function(){
        var that =this
        var data = {'type':4}
        $api.get('banner',data).then(res => {
            //请求成功
            if(res.data.code == 1){
              that.setData({
                banner:res.data.data[0].img
              })
            }
        }).catch(err => {
            //请求失败
            //console.log('error','请求失败');
        })
    },
    getNes:function(){
        var that =this
        var data = {
            page:this.data.page
        }
        var list = this.data.list
        $api.get('news/list',data).then(res => {
            //请求成功
            if(res.data.code == 1){
                if(res.data.data.last_page <= that.data.page){
                    that.setData({
                        pageShow:false
                    })
                }
                wx.hideLoading()
                if(that.data.page == 1){
                    that.setData({
                        list:res.data.data.data
                    })
                }else{
                    that.setData({
                        list:list.concat(res.data.data.data)
                    })
                }
            }
        }).catch(err => {
            //请求失败
            console.log('error','请求失败');
        })
    },
    toNewsInfo:function(e){
        var id =e.target.dataset.id
        wx.navigateTo({
            url: '/pages/news/info?id='+id
        })
    },
    onReachBottom: function(t){
        if(this.data.pageShow){
            //当前不是最后一页可加载
            this.setData({
                page: this.data.page + 1,
            })
            wx.showLoading()
            this.getNes();
        }
    },
    onShareAppMessage: function() {
        return {
            title: '行业资讯--天意美业直通车',
            path: "/pges/index/index?puid="+wx.getStorageSync('uid')
        };
    }
});